// SimpleTest.cpp : main project file.

#include "stdafx.h"

using namespace System;
using namespace System::IO;
using namespace zencat::sqlite;

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");

	// start up

    String^ testDirectory = 
		Path::Combine(Environment::GetFolderPath(Environment::SpecialFolder::MyDocuments), "simpletest_zencat_sqlite");
	if (!Directory::Exists(testDirectory)) {
	    Directory::CreateDirectory(testDirectory);
	}

	String^ traceFileName = Path::Combine(testDirectory, "trace_1.log");
    Sqlite::TraceFileName = traceFileName;
	Sqlite::TraceDestination = TrcDestination::TraceToFile;
	Sqlite::TraceLevel = TrcLevel::Verbose;

	Sqlite::TraceLine(gcnew String(L"Hi!"));

    Sqlite::Initialize();

	// switch trace file name
    traceFileName = Path::Combine(testDirectory, "trace_2.log");
    Sqlite::TraceFileName = traceFileName;
	
	Sqlite::TraceLine(gcnew String(L"Hi!"));

	String^ testDb = Path::Combine(testDirectory, "test.database");
	IDatabase^ db = Sqlite::OpenDatabase(testDb);
	if (db->WasCreated) {
		db->ExecuteSql(gcnew String(L"CREATE TABLE t1 (id INTEGER PRIMARY KEY, i1 INTEGER, t1 TEXT); "));
	}

	IStatement^ stmt = db->CreateStatement(gcnew String(L"Insert into t1 (i1, t1) values(?, ?);"));
	stmt->Reset();
	stmt->BindInt64ToParameter(1, 1);
	stmt->BindStringToParameter(gcnew String(L"Hallo"), 2);
	stmt->Step();

	stmt->Reset();
	stmt->BindInt64ToParameter(2, 1);
	stmt->BindStringToParameter(gcnew String(L"Hallo 2"), 2);
	stmt->Step();

	IStatement^ stmt2 = db->CreateStatement(gcnew String(L"Select * from t1 order by id;"));
	stmt2->Reset();
	while (stmt2->Step());

	// shut down
	Sqlite::Shutdown();
	Sqlite::TraceLevel = TrcLevel::Off;


    return 0;
}
